package com.argticling.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.argticling.dao.impl.hibernate.HibernateUtil;
import com.argticling.dao.interf.ReaderDAO;
import com.argticling.model.Reader;
import com.argticling.model.User;

/**
 * Created by IntelliJ IDEA. User: santor Date: 08-nov-2010 Time: 19:29:04 To
 * change this template use File | Settings | File Templates.
 */
public class ReaderDAOImplementation extends AbstractDAOImplementation
		implements ReaderDAO {

	public void deleteReader(String userMail, String readerMail) {
		HibernateUtil hu = new HibernateUtil();
		Reader reader = (Reader) hu.getSession().createQuery(
				"from Reader as r where r.readerMail = ? AND r.userMail = ? ")
				.setString(0, readerMail).setString(1, userMail).uniqueResult();

		HibernateUtil.getSession().delete(reader);
		HibernateUtil.commitTransaction();
	}

	public Reader getReader(String readerMail, String userMail) {
		HibernateUtil hu = new HibernateUtil();
		
		Reader reader = (Reader) hu.getSession().createQuery(
						"from Reader as r where r.readerMail = ? AND r.userMail = ? ")
				.setString(0, readerMail).setString(1, userMail).uniqueResult();

		return reader;
	}


	public void newReader(String userMail, String readerMail) {
		Reader reader = new Reader(userMail, readerMail);
		persist(reader);
	}

	public List<String> getReaders(String username) {
		List<Reader> readers = (List<Reader>) HibernateUtil.getSession()
				.createQuery("from Reader as "
								+ "r where r.userMail = '" + username + "'").list();
		
		List<String> readersMails = new ArrayList<String>();
		
		for (Reader r : readers) {
			readersMails.add(r.getReaderMail());
		}
		
		return readersMails;
	}
	
	public List<Reader> getReaderList(String username) {
		HibernateUtil hu = new HibernateUtil();
		List<Reader> readers = (List<Reader>) hu.getSession()
				.createQuery("from Reader as "
								+ "r where r.userMail = '" + username + "'").list();
		
		return readers;
	}
}
